package com.tengju.bff.interfaces.shared.servlet;

import com.beust.jcommander.internal.Lists;
import com.tengju.bff.interfaces.shared.InterfaceCode;
import com.tengju.bff.interfaces.shared.InterfaceException;
import com.tengju.user.domain.model.club.ClubId;
import com.tengju.user.domain.model.login.UserType;
import com.tengju.user.domain.model.user.StaffId;
import com.tengju.user.domain.model.user.StarLevelEnum;
import com.tengju.user.domain.model.user.UserId;
import com.tengju.user.domain.model.user.UserIdCode;
import com.tengju.user.domain.shared.DomainException;
import com.tengju.user.domain.shared.DomainExceptionCode;
import lombok.Data;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.stream.Collectors;

import org.apache.commons.lang3.StringUtils;
import org.springframework.util.CollectionUtils;

@Data
public class UserServletContext {
    /**
     * http请求
     */
    public HttpServletRequest request;

    /**
     * http响应
     */
    public HttpServletResponse response;
    /**
     * 用户ID
     */
    private long userId;

    /**
     * 后台用户主键ID
     */
    private Long staffId;

    /**
     * 后台用户账号
     */
    private String username;

    /**
     * 后台用户实际姓名
     */
    private String realName;

    /**
     * 用户类型
     */
    private UserType userType;

    /**
     * idCode
     */
    private UserIdCode userIdCode;

    /**
     * clubIds
     */
    private List<ClubId> clubIds;

    /**
     * sec线
     */
    private List<UserIdCode> genSecIdCodes;

    /**
     * 角色code列表
     */
    private List<String> roleCodes;
    /**
     * 请求来源ip
     */
    private String ip;

    /**
     * 源请求参数k/v
     */
    private Map<String, Object> paramMap;

    /**
     * 解密参数key
     */
    private Set<String> encryptParam = new HashSet<>();

    /**
     * app版本
     */
    private String version;

    /**
     * weex版本
     */
    private String weexVersion;

    /**
     * app来源
     */
    private String from;

    /**
     * 请求客户端  ios | android
     */
    private String platform;

    /**
     * 机型  iphone7 | 华为Mate40
     */
    private String device;

    private String requestPath;


    public UserId getUserId() {
        if (userType == UserType.USER) {
            return new UserId(userId);
        } else {
            throw DomainException.error(DomainExceptionCode.INVALID_DATA);
        }
    }

    public StaffId getStaffId() {
        if (userType == UserType.STAFF) {
            return new StaffId(staffId);
        } else {
            throw DomainException.error(DomainExceptionCode.INVALID_DATA);
        }
    }


    public UserId needUserId() throws InterfaceException {
        if (userId <= 0) {
            throw new InterfaceException(InterfaceCode.NOT_LOGIN);
        }

        return getUserId();
    }


    public StaffId needStaffId() throws InterfaceException {
        if (staffId <= 0) {
            throw new InterfaceException(InterfaceCode.NOT_LOGIN);
        }

        return getStaffId();
    }


    public UserIdCode needIdCode() throws InterfaceException {
        if (userIdCode == null) {
            throw new InterfaceException(InterfaceCode.INSUFFICIENT_AUTHORITY);
        } else {
            return new UserIdCode((userIdCode.getValue()));
        }
    }

    public UserIdCode getIdCode() {
        if (userIdCode == null) {
            return null;
        } else {
            return new UserIdCode((userIdCode.getValue()));
        }
    }

    public String getIp() {
        if (StringUtils.isBlank(ip)) {
            return "";
        } else {
            return ip;
        }
    }

    public List<com.tengju.data.domain.model.user.UserIdCode> getDataGenSecIdCodes() {

        if (CollectionUtils.isEmpty(genSecIdCodes)) {
            return Lists.newArrayList();
        }

        return genSecIdCodes.stream()
                .map(UserIdCode::getValue).map(com.tengju.data.domain.model.user.UserIdCode::new)
                .collect(Collectors.toList());
    }

    public String getFrom() {
        return FromEnum.getValueByKey(from);
    }



    @Override
    public String toString() {
        return "UserServletContext{" +
                "userId=" + userId +
                ", staffId=" + staffId +
                ", username='" + username + '\'' +
                ", realName='" + realName + '\'' +
                ", userType=" + userType +
                ", userIdCode=" + userIdCode +
                ", ip='" + ip + '\'' +
                ", version='" + version + '\'' +
                ", weexVersion='" + weexVersion + '\'' +
                ", from='" + from + '\'' +
                ", platform='" + platform + '\'' +
                ", device='" + device + '\'' +
                '}';
    }
}
